<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      @close="closeDialog()"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="产品" prop="productNumber">
        <template>
          <el-select v-model="dataForm.projectNumber" :disabled="dataForm.id != 0" placeholder="请选择" @change="getProductInfo()">
            <el-option
                v-for="item in options"
                :key="item.prProjectNumber"
                :label="item.prProjectName"
                :value="item.prProjectNumber">
            </el-option>
          </el-select>
        </template>
      </el-form-item>

      <!-- 绑定每个输入框到 productDataFrom 的相应字段 -->
      <el-form-item v-for="(data, index) in productData"
                    :label="data.project"
                    :key="index">
        <!-- 绑定每个输入框的值到 productDataFrom 对应的字段 -->
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>{{data.unit}}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-input v-model="productDataFrom[data.characteristic]" :placeholder="data.project" style="width:200px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        projectNumber: '',  // 用来存储选择的项目编号
      },
      options: [],
      productDataFrom: {
        model: "",
        construction: "",
        outline: "",
        job: "",
        operange: "",
        tilldepth: "",
        cutter: "",
        cutterconn: "",
        drive: "",
        transportation: "",
        gearbox: "",
        knife: "",
        knifeMax: "",
        knifenum: "",
        tooltype: "",
        depthlimit: "",
        suppress: "",
        power: "",
        rotate: "",
      },
      productData: [], // 存储产品数据
      dataRule: {
        personEnterprise: [
          { required: true, message: '企业负责人不能为空', trigger: 'blur' }
        ],
        timeEnter: [
          { required: true, message: '录入时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getProductData()
    this.getProductNumber()
  },
  methods: {
    getProductInfo(){
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${this.dataForm.projectNumber}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataForm.product = data.productInfo.prProjectName
          this.dataForm.model = data.productInfo.prProducerModel

        }else {
          this.$message.error(data.msg)
        }
      })
    },    getProductNumber() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/product/getProjectNumber'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.options = data.productList
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getProductData() {
      return this.$http({
        url: this.$http.adornUrl('/reportapproval/productspecificationdata/getProductData'),
        method: 'get',
        params: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          console.log(data.list)
          this.productData = data.list
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/productspecification/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 0) {
              console.log(data.productSpecification)
              this.dataForm.projectNumber = data.productSpecification.projectNumber
              this.productDataFrom = data.productSpecification.dataFromSub
              // this.dataForm.personEnterprise = data.productSpecification.personEnterprise
              // this.dataForm.timeEnter = data.productSpecification.timeEnter
              // 更新 productData 与返回的数据
              // this.productData.forEach((item, index) => {
              //   const product = data.productSpecification.find(p => p.projectNumber === item.prProjectNumber)
              //   if (product) {
              //     this.productData[index].designValue = product.designValue || ''
              //   }
              // })
              // 填充 productDataFrom
              this.fillProductDataFrom()
            }
          })
        }
      })
    },
    // 填充 productDataFrom 的数据
    fillProductDataFrom() {
      // 通过 reduce 合并 productData 中的数据到 productDataFrom 中
      this.productData.reduce((acc, item) => {
        if (item.project) {
          acc[item.project] = item.designValue || '';  // 填充设计值或为空
        }
        return acc;
      }, this.productDataFrom);
    },
    closeDialog() {
      this.resetForm();
      this.$emit('refreshDataList');
    },
    resetForm() {
      // this.$refs.form.resetFields();
      this.dataForm = {};
      this.productDataFrom = {};
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/productspecification/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              projectNumber: this.dataForm.projectNumber,
              dataFromSub: this.productDataFrom
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
